﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace The_LarX
{
    public class InterpreterArguments
    {
        /// <summary>
        /// The arguments passed to the method
        /// </summary>
        public string Arguments;

        private int numberOfArguments;

        /// <summary>
        /// The number of arguments present in Arguments string
        /// </summary>
        public int ArgumentsNumber
        {
            get { return numberOfArguments; }
        }

        /// <summary>
        /// The values
        /// </summary>
        public List<object> Values;

        private LarWebPage WebPage;

        public InterpreterArguments(LarWebPage page, string args)
        {
            this.WebPage = page;
            this.Arguments = args;
            TransformArgsInValues();
        }

        private void TransformArgsInValues()
        {
            this.Values = new List<object>();
            string[] arr = Arguments.Split(',');
            numberOfArguments = arr.Length;

            for (int i = 0; i < arr.Length; i++)
            {
                string finalArr = arr[i];
                while (finalArr.Contains(")"))
                    finalArr = finalArr.Replace(")", "");
                while (finalArr.Contains("("))
                    finalArr = finalArr.Replace("(", "");
                if (finalArr.Contains('"'))
                {
                    // If it has quotes, must be string
                    string s = finalArr;
                    if (s.Contains("$"))
                    {
                        string[] array = s.Split('$');
                        for (int k = 1; k < array.Length; k++)
                        {
                            string varName = "$";
                            for (int j = 0; j < array[k].Length; j++)
                            {
                                if (!StringUtil.IsSymbol(array[k][j]))
                                {
                                    varName += array[k][j];
                                }
                            }
                        }
                    }
                    this.Values.Add(s);
                }
                else
                {
                    double outDo;
                    bool IsNumber = double.TryParse(finalArr, out outDo);
                    if (IsNumber)
                    {
                        if (finalArr.Contains("."))
                        {
                            // It's a float!
                            float outFloat;
                            float.TryParse(finalArr, out outFloat);
                            this.Values.Add(outFloat);
                        }
                        else
                        {
                            // It's an int!
                            int outInt;
                            int.TryParse(finalArr, out outInt);
                            this.Values.Add(outInt);
                        }
                    }
                    else
                    {
                        /// If it's nothing like that, it MUST be bool
                        if (finalArr.ToLower().Contains("true"))
                            this.Values.Add(true);
                        if (finalArr.ToLower().Contains("false"))
                            this.Values.Add(false);
                    }
                }
            }
        }
    }
}
